import masterMenuMock from '@/mock/master'
import merchantMenuMock from '@/mock/merchant'

// 确保每个菜单项都有meta属性
function ensureMenuMeta(menus) {
  if (!menus || !Array.isArray(menus)) return [];
  
  return menus.map(menu => {
    // 确保meta属性存在
    if (!menu.meta) {
      menu.meta = {
        type: 'M',
        icon: 'icon-default',
        title: menu.name || '菜单项',
        hidden: false,
        hiddenBreadcrumb: false
      };
    }
    
    // 递归处理子菜单
    if (menu.children && Array.isArray(menu.children)) {
      menu.children = ensureMenuMeta(menu.children);
    } else {
      menu.children = [];
    }
    
    return menu;
  });
}

// 处理不同格式的 mock 数据
function processMockData(mockData) {
  // 检查是否是 API 响应格式
  if (mockData && mockData.data && mockData.data.routers) {
    // merchant.js 格式
    return {
      codes: mockData.data.codes || ['*'],
      roles: mockData.data.roles || ['admin'],
      routers: ensureMenuMeta(mockData.data.routers),
      user: mockData.data.user || {
        id: 1,
        username: 'admin',
        nickname: '管理员',
        avatar: 'https://avatars.githubusercontent.com/u/20942571?s=200&v=4',
        backend_setting: JSON.stringify({
          mode: 'light',
          tag: true,
          menuCollapse: false,
          menuWidth: 230,
          layout: 'classic',
          skin: 'mine',
          i18n: false,
          language: 'zh_CN',
          animation: 'ma-slide-down',
          color: '#165dff'
        })
      },
      menus: ensureMenuMeta(mockData.data.routers)
    };
  } else {
    // master.js 格式
    return {
      codes: ['system:user:index', 'system:user:update'],
      roles: ['admin'],
      routers: ensureMenuMeta(mockData),
      user: {
        id: 1,
        username: 'admin',
        nickname: '管理员',
        avatar: 'https://avatars.githubusercontent.com/u/20942571?s=200&v=4',
        backend_setting: JSON.stringify({
          mode: 'light',
          tag: true,
          menuCollapse: false,
          menuWidth: 230,
          layout: 'classic',
          skin: 'mine',
          i18n: false,
          language: 'zh_CN',
          animation: 'ma-slide-down',
          color: '#165dff'
        })
      },
      menus: ensureMenuMeta(mockData)
    };
  }
}

// 处理菜单数据
const processedMasterData = processMockData(masterMenuMock);
const processedMerchantData = processMockData(merchantMenuMock);

// 模拟用户数据
const mockUserData = {
  master: processedMasterData,
  merchant: processedMerchantData
}

// 获取模拟数据
export function getMockUserData(type) {
  console.log('获取模拟数据类型:', type);
  return mockUserData[type] || mockUserData.master;
}
